package jdbc.resultset_;

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;

/**
 * @author DCSGO
 * @version 1.0
 * 结果集
 * 执行查询语句后的返回结果
 */
@SuppressWarnings({"all"})
public class ResultSet_ {
    public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {
        Properties properties = new Properties();
        properties.load(new FileInputStream("src\\mysql.properties"));
        String url = properties.getProperty("url");
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String Driver = properties.getProperty("Driver");
        //1.注册驱动
        Class.forName(Driver);
        //2.获取连接
        Connection connection = DriverManager.getConnection(url, user, password);
        //3.获取statement
        Statement statement = connection.createStatement();
        //4.执行查询
        String sql = "select * from performer";
        ResultSet resultSet = statement.executeQuery(sql);
        /*
         * +----+-----------+------+------------+--------------+
         * | id | name      | sex  | birthday   | phone_number |
         * +----+-----------+------+------------+--------------+
         * |  1 | 刘德华     | 男   | 1990-04-25 | 11223344556  |
         * |  2 | jack      | 男   | 2000-09-30 | 12345678901  |
         * +----+-----------+------+------------+--------------+
         */
        //遍历查询返回的结果集
        while (resultSet.next()) {//next() 方法返回结果集中的某列的信息 每执行一次就先后偏移1 没有数据时返回 false
            int id = resultSet.getInt(1);//从第一列获取一个int
            String name = resultSet.getString(2);//从第二列获取一个字符串
            String sex = resultSet.getString(3);
            Date date = resultSet.getDate(4);
            String phoneNumber = resultSet.getString(5);
            System.out.println(id + "\t" + name + "\t" + sex + "\t" + date + "\t" + phoneNumber);
        }
        //1	刘德华	男	1990-04-25	11223344556
        //3	jack	男	2000-09-30	12345678901
        //resultSet 中有一个 rowData 用于存放行相关数据其中的 rows(ArrayList) 存放行中具体的数据

        //5.关闭资源
        resultSet.close();
        statement.close();
        connection.close();
    }
}
